(function(win, doc) {
  function Compute(options) {
    this.params = options || {};
    this.index = 0;
    // this.id = typeof container === 'object' ? container : document.querySelectorAll(container);
    function normalizeEventName(eventName) {
      if (eventName.indexOf('on') !== 0) {
        if (eventName[0] !== eventName[0].toUpperCase()) {
          eventName = 'on' + eventName[0].toUpperCase() + eventName.substring(1);
        } else {
          eventName = 'on' + eventName;
        }
      }
      return eventName;
    }
    this.emitterEventListeners = {};
    this.emit = function(eventName) {
      // Trigger callbacks
      if (this.params[eventName]) {
        this.params[eventName](arguments[1], arguments[2], arguments[3], arguments[4], arguments[5]);
      }
      var i;
      // Trigger events
      if (this.emitterEventListeners[eventName]) {
        for (i = 0; i < this.emitterEventListeners[eventName].length; i++) {
          this.emitterEventListeners[eventName][i](arguments[1], arguments[2], arguments[3], arguments[4], arguments[5]);
        }
      }
    };
    this.on = function(eventName, handler) {
      eventName = normalizeEventName(eventName);
      if (!this.emitterEventListeners[eventName]) this.emitterEventListeners[eventName] = [];
      this.emitterEventListeners[eventName].push(handler);
      return this;
    };
    this.init();
    this.emit('oninit', this);
  };
  Compute.prototype = {
    init: function() {
      var _this = this;
      this.subtract = document.querySelectorAll('.quantity_subtract');
      this.add = document.querySelectorAll('.quantity_add');
      this.input = document.querySelectorAll('.quantity_input');
      this.inputs();
      this.adds();
      this.subtracts();
    },
    inputs: function() {
      var _this = this;
      [].forEach.call(this.input, function(ele, index) {
        ele.addEventListener('input', function() {
          var val = this.value.trim().replace(/\D/, '').replace(/^0/, '');
          this.value = val;
          _this.index = index;
          _this.emit('onclick', _this, this);
        }, false);
        ele.addEventListener('blur', function() {
          var val = this.value.trim().replace(/\D/, '').replace(/^0/, '');
          if (val == '') {
            this.value = '1';
            _this.emit('onclick', _this, this);
          }
        }, false);
      });
    },
    adds: function() {
      var _this = this;
      [].forEach.call(this.add, function(ele, index) {
        ele.addEventListener('click', function() {
          var inputs = this.previousElementSibling,
            val = +inputs.value.trim() + 1;
          inputs.value = val;
          _this.index = index;
          _this.emit('onclick', _this, this);
        }, false);
      })
    },
    subtracts: function() {
      var _this = this;
      [].forEach.call(this.subtract, function(ele, index) {
        ele.addEventListener('click', function() {
          var inputs = this.nextElementSibling,
            val = +(inputs.value.trim());
          _this.index = index;
          if (val <= 1) {
            _this.emit('onclick', _this, this);
            return;
          }
          inputs.value = val - 1;
          _this.emit('onclick', _this, this);
        }, false);
      })
    },
    computeMoney: function() {

    }
  };
  window.Compute = Compute;
}(window, document));
